import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/login.vue'
import Home from '../components/home.vue'
import Users from '../components/user/user.vue'
import Rights from '../components/power/Rights.vue'
import Roles from '../components/power/Roles.vue'
import Cate from '../components/goods/cate.vue'
import Params from '../components/goods/params.vue'
import List from '../components/goods/list.vue'
import Add from '../components/goods/add.vue'
import Order from '../components/order/order.vue'
import Report from '../components/report/report.vue'
// import { Router } from 'express';
import Welcome from '../components/welcome.vue'
Vue.use(VueRouter)
// export default new VueRouter({
//   routes: [
//     { path: '/', redirect: '/login' },
//     { path: '/login', component: Login },
//     { path: '/home', component: Home }
//   ]
// })
// 路由守卫
// 先把之前导出的路由规则存为一个变量,再导出
const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
      path: '/home', component: Home,
      redirect: '/Welcome', children: [
        { path: '/welcome', component: Welcome },
        { path: '/users', component: Users },
        { path: '/rights', component: Rights },
        { path: '/roles', component: Roles },
        { path: '/categories', component: Cate },
        { path: '/params', component: Params },
        { path: '/goods', component: List },
        { path: '/goods/add', component: Add },
        { path: '/orders', component: Order },
        { path: '/reports', component: Report }

      ]
    },

  ]
})
router.beforeEach((to, from, next) => {
  // to是将要访问的路径
  // from是从哪个路径跳转过来的
  // next是一个函数 ，表示放行  next()放行    next('/login') 强制跳转
  if (to.path === '/login') return next()    //如果访问登录页就直接放行
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')  //如果不存在token则强制跳转到登录首页
  next()  //否则就是有token，就直接放行
})
export default router